#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import base64
from tkinter import *
from tkinter import filedialog
from tkinter.messagebox import showinfo

import time

LOG_LINE_NUM = 0
files = []


# 获取当前时间
def get_current_time():
    current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    return current_time


class MyGui:
    def __init__(self, init_window_name):
        self.init_window_name = init_window_name

    # 设置窗口
    def set_init_window(self):
        # self.init_window_name.title("可疑币筛选工具_v1.0")  # 窗口名
        # self.init_window_name.geometry('320x160+10+10')                         #290 160为窗口大小，+10 +10 定义窗口弹出时的默认展示位置
        self.init_window_name.geometry('650x600+50+50')
        # self.init_window_name["bg"] = "pink"                                    #窗口背景色，其他背景色见：blog.csdn.net/chl0000/article/details/7657887
        # self.init_window_name.attributes("-alpha",0.9)                          #虚化，值越小虚化程度越高
        # logo

        f = Menu(self.init_window_name)  # 创建根菜单
        self.init_window_name['menu'] = f
        f1 = Menu(f)
        f1.add_command(label='查看帮助文档', command=self.help)
        f.add_cascade(label='帮助', menu=f1)  # 创建顶级菜单栏，并关联子菜单

        img_data = base64.b64decode(
            b'R0lGODlhzwA7APcAAAAAAAAAMwAAZgAAmQAAzAAA/wArAAArMwArZgArmQArzAAr/wBVAABVMwBVZgBVmQBVzABV/wCAAACAMwCAZgCAmQCAzACA/wCqAACqMwCqZgCqmQCqzACq/wDVAADVMwDVZgDVmQDVzADV/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA/zMrADMrMzMrZjMrmTMrzDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOAmTOAzDOA/zOqADOqMzOqZjOqmTOqzDOq/zPVADPVMzPVZjPVmTPVzDPV/zP/ADP/MzP/ZjP/mTP/zDP//2YAAGYAM2YAZmYAmWYAzGYA/2YrAGYrM2YrZmYrmWYrzGYr/2ZVAGZVM2ZVZmZVmWZVzGZV/2aAAGaAM2aAZmaAmWaAzGaA/2aqAGaqM2aqZmaqmWaqzGaq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vHLMbeijh0LoonM6FBn9NmMGPtiJgcp5kZGE4NnTLcPSkWqi2/aXhhhmSlG1WOFAcPfRt4Ar0g5KAep7BfAEkAahohhZ1NciCbimE1N5eZu+tAE59BznkkACEDb+U9g0NCeNABBBkbrVEFwMP+ASkLjYA8AzkIAMABdDs9HqqsIbKB4JN5Z5IzK8GMUkdPEVzbFogLpXU6UoT44KeAzndJE/WqmCRgei2ePXAgaJDGJliJOEmmAqUxjGtPtzPSmNY1pJtDQUplOYqaTwERPEcdTntIUpywFalJtStOW7nRxUC1qVKEa1Zty66dEnYQYyvmQFYopinFCDgJwECAuyWUrmlhcf0R4vuKlKytKgeZE8TQ0eyGHlSAEKxiHeDQAqMVPxRwUDkwFqn7p0lT7eNBWMzEJwFnJRcrAZzENWthipS8NwlqM2SbYrwTVch846F1Jh6GJxpSUGJ4qTQAUGBnRJchZHuRqRZi3q5v/XTEBJMTtH3mGgEo+BH9c2sxl4OIcxAA3o+BbDHyWIQN1GmgZj4wGmpJ0kMG1CRp5YxQctPXGKDWmIAYt1kAEcDdpXmcgBg2vSLZytTrpda6tFM9pLoI/ChlMMsTFjHi7qwwDyUBacdEcNk0XWYGIYTD4Y5ti7IYZtYxBS7oMzX338cJ6HAC8jPoreg+iS4aNVyDjXEk0upKDGdzIRx/KC1nMshFloEEyW10dYpyaKNROgkVi6MpAtqNKCq1sH4YBMnzQcBkfZqJ7+/ChvooJHxbHGMj7GEYaokEMxD6spXPJMRpUeZkYKMM0XKFQV9QCjbhIAjVokC1cgzLLhKh5I81wjrOc50znOtv5znjOs573zOc++/nPgA60oAdN6EIb+tCICE60ohfdlIAAADs=')
        # 注意：如果是"data:image/jpg:base64,"，那你保存的就要以png格式，如果是"data:image/png:base64,"那你保存的时候就以jpg格式。
        with open('logo1.gif', 'wb') as f:
            f.write(img_data)
            f.close()
        # 插入图片
        photo = PhotoImage(file="logo1.gif")
        label = Label(image=photo)
        label.image = photo
        label.grid(row=0, column=12)

        # 选择单个文件
        self.fileItem = Button(self.init_window_name, text="选择文件", bg="lightblue", width=10,
                               command=self.selectXls)  # 调用内部方法  加()为直接调用
        self.fileItem.grid(row=1, column=2)
        # 选择文件夹
        self.fileDir = Button(self.init_window_name, text="选择文件夹", bg="lightblue", width=10,
                              command=self.selectFiledir)
        self.fileDir.grid(row=1, column=5)

        # 标签
        self.init_data_label = Label(self.init_window_name, text="待筛选文件")
        self.init_data_label.grid(row=4, column=2)
        self.result_data_label = Label(self.init_window_name, text="输出文件")
        self.result_data_label.grid(row=4, column=12)

        self.log_label = Label(self.init_window_name, text="日志")
        self.log_label.grid(row=9, column=2)
        self.footer_label1 = Label(self.init_window_name, text='兴业银行威海分行 运营管理部')
        self.footer_label1.grid(row=15, column=5)

        # 文本框
        self.init_data_Text = Text(self.init_window_name, width=40, height=20)  # 原始数据录入框
        self.init_data_Text.grid(row=5, column=1, rowspan=4, columnspan=6)
        self.result_data_Text = Text(self.init_window_name, width=40, height=20)  # 处理结果展示
        self.result_data_Text.grid(row=5, column=10, rowspan=4, columnspan=6)
        self.log_data_Text = Text(self.init_window_name, width=92, height=10)  # 日志框
        self.log_data_Text.grid(row=10, column=1, rowspan=3, columnspan=12)

        # 按钮
        self.str_trans_to_md5_button = Button(self.init_window_name, text="开始处理", bg="lightblue", width=10,
                                              command=self.filterExt)  # 调用内部方法  加()为直接调用
        self.str_trans_to_md5_button.grid(row=7, column=9)

    def help(self):
        showinfo(title='提示', message=' 只支持XLS和XLSX格式文档处理 \n  表格中F列必须未冠字号， K列必须为币种 ')

    def selectXls(self):
        filepath = filedialog.askopenfilename(title="选择要处理的文件", filetypes=[('EXCEL文件', ('*.xls', '*.xlsx'))])
        global files
        files.append(filepath)
        self.init_data_Text.insert(END, filepath + "\n")

    def selectFiledir(self):
        folderpath = filedialog.askdirectory()
        global files
        filenames = os.listdir(folderpath)
        for filepath, dirnames, filenames in os.walk(folderpath):
            for filename in filenames:
                fullname = os.path.join(filepath, filename).replace('\\', '/')
                if fullname.endswith('xls') or fullname.endswith('xlsx'):
                    files.append(fullname)
                    self.init_data_Text.insert(END, fullname + "\n")


    # 日志动态打印
    def write_log_to_Text(self, logmsg):
        global LOG_LINE_NUM
        current_time = get_current_time()
        logmsg_in = str(current_time) + " " + str(logmsg) + "\n"  # 换行
        if LOG_LINE_NUM <= 100:
            self.log_data_Text.insert(END, logmsg_in)
            LOG_LINE_NUM = LOG_LINE_NUM + 1
        else:
            self.log_data_Text.delete(1.0, 2.0)
            self.log_data_Text.insert(END, logmsg_in)

    def filterExt(self):
        global files
        self.init_data_Text.delete(1.0, END)
        for i in files:
            self.write_log_to_Text(i + " 已处理！ ")

        files = []


def gui_start():
    init_window = Tk()  # 实例化出一个父窗口
    init_window.title("可疑币筛选工具_v1.0     联系人：刘军鹏 17327713618")
    init_window.resizable(0, 0)
    myui = MyGui(init_window)
    # 设置根窗口默认属性
    myui.set_init_window()
    init_window.mainloop()  # 父窗口进入事件循环，可以理解为保持窗口运行，否则界面不展示


if __name__ == '__main__':
    gui_start()
